home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / utils / unix / unzip-5.12 / explode.c < prev    next >
C/C++ Source or Header  |  1994-08-24  |  29KB  |  826 lines

  1. /* explode.c -- put in the public domain by Mark Adler
  2.    version c13, 25 August 1994 */
  3.  
  4.  
  5. /* You can do whatever you like with this source file, though I would
  6.    prefer that if you modify it and redistribute it that you include
  7.    comments to that effect with your name and the date.  Thank you.
  8.  
  9.    History:
  10.    vers    date          who           what
  11.    ----  ---------  --------------  ------------------------------------
  12.     c1   30 Mar 92  M. Adler        explode that uses huft_build from inflate
  13.                                     (this gives over a 70% speed improvement
  14.                                     over the original unimplode.c, which
  15.                                     decoded a bit at a time)
  16.     c2    4 Apr 92  M. Adler        fixed bug for file sizes a multiple of 32k.
  17.     c3   10 Apr 92  M. Adler        added a little memory tracking if DEBUG
  18.     c4   11 Apr 92  M. Adler        added NOMEMCPY do kill use of memcpy()
  19.     c5   21 Apr 92  M. Adler        added the WSIZE #define to allow reducing
  20.                                     the 32K window size for specialized
  21.                                     applications.
  22.     c6   31 May 92  M. Adler        added typecasts to eliminate some warnings
  23.     c7   27 Jun 92  G. Roelofs      added more typecasts.
  24.     c8   17 Oct 92  G. Roelofs      changed ULONG/UWORD/byte to ulg/ush/uch.
  25.     c9   19 Jul 93  J. Bush         added more typecasts (to return values);
  26.                                     made l[256] array static for Amiga.
  27.     c10   8 Oct 93  G. Roelofs      added used_csize for diagnostics; added
  28.                                     buf and unshrink arguments to flush();
  29.                                     undef'd various macros at end for Turbo C;
  30.                                     removed NEXTBYTE macro (now in unzip.h)
  31.                                     and bytebuf variable (not used); changed
  32.                                     memset() to memzero().
  33.     c11   9 Jan 94  M. Adler        fixed incorrect used_csize calculation.
  34.     c12   9 Apr 94  G. Roelofs      fixed split comments on preprocessor lines
  35.                                     to avoid bug in Encore compiler.
  36.     c13  25 Aug 94  M. Adler        fixed distance-length comment (orig c9 fix)
  37.  */
  38.  
  39.  
  40. /*
  41.    Explode imploded (PKZIP method 6 compressed) data.  This compression
  42.    method searches for as much of the current string of bytes (up to a length
  43.    of ~320) in the previous 4K or 8K bytes.  If it doesn't find any matches
  44.    (of at least length 2 or 3), it codes the next byte.  Otherwise, it codes
  45.    the length of the matched string and its distance backwards from the
  46.    current position.  Single bytes ("literals") are preceded by a one (a
  47.    single bit) and are either uncoded (the eight bits go directly into the
  48.    compressed stream for a total of nine bits) or Huffman coded with a
  49.    supplied literal code tree.  If literals are coded, then the minimum match
  50.    length is three, otherwise it is two.
  51.    
  52.    There are therefore four kinds of imploded streams: 8K search with coded
  53.    literals (min match = 3), 4K search with coded literals (min match = 3),
  54.    8K with uncoded literals (min match = 2), and 4K with uncoded literals
  55.    (min match = 2).  The kind of stream is identified in two bits of a
  56.    general purpose bit flag that is outside of the compressed stream.
  57.    
  58.    Distance-length pairs for matched strings are preceded by a zero bit (to
  59.    distinguish them from literals) and are always coded.  The distance comes
  60.    first and is either the low six (4K) or low seven (8K) bits of the
  61.    distance (uncoded), followed by the high six bits of the distance coded.
  62.    Then the length is six bits coded (0..63 + min match length), and if the
  63.    maximum such length is coded, then it's followed by another eight bits
  64.    (uncoded) to be added to the coded length.  This gives a match length
  65.    range of 2..320 or 3..321 bytes.
  66.  
  67.    The literal, length, and distance codes are all represented in a slightly
  68.    compressed form themselves.  What is sent are the lengths of the codes for
  69.    each value, which is sufficient to construct the codes.  Each byte of the
  70.    code representation is the code length (the low four bits representing
  71.    1..16), and the number of values sequentially with that length (the high
  72.    four bits also representing 1..16).  There are 256 literal code values (if
  73.    literals are coded), 64 length code values, and 64 distance code values,
  74.    in that order at the beginning of the compressed stream.  Each set of code
  75.    values is preceded (redundantly) with a byte indicating how many bytes are
  76.    in the code description that follows, in the range 1..256.
  77.  
  78.    The codes themselves are decoded using tables made by huft_build() from
  79.    the bit lengths.  That routine and its comments are in the inflate.c
  80.    module.
  81.  */
  82.  
  83. #include "unzip.h"      /* must supply slide[] (uch) array and NEXTBYTE macro */
  84.  
  85. #ifndef WSIZE
  86. #  define WSIZE 0x8000  /* window size--must be a power of two, and */
  87. #endif                  /* at least 8K for zip's implode method */
  88.  
  89.  
  90. struct huft {
  91.   uch e;                /* number of extra bits or operation */
  92.   uch b;                /* number of bits in this code or subcode */
  93.   union {
  94.     ush n;              /* literal, length base, or distance base */
  95.     struct huft *t;     /* pointer to next level of table */
  96.   } v;
  97. };
  98.  
  99. /* Function prototypes */
  100. /* routines from inflate.c */
  101. extern unsigned hufts;
  102. int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *,
  103.                    struct huft **, int *));
  104. int huft_free OF((struct huft *));
  105.  
  106. /* routines here */
  107. int get_tree OF((unsigned *, unsigned));
  108. int explode_lit8 OF((struct huft *, struct huft *, struct huft *,
  109.                      int, int, int));
  110. int explode_lit4 OF((struct huft *, struct huft *, struct huft *,
  111.                      int, int, int));
  112. int explode_nolit8 OF((struct huft *, struct huft *, int, int));
  113. int explode_nolit4 OF((struct huft *, struct huft *, int, int));
  114. int explode OF((void));
  115.  
  116.  
  117. /* The implode algorithm uses a sliding 4K or 8K byte window on the
  118.    uncompressed stream to find repeated byte strings.  This is implemented
  119.    here as a circular buffer.  The index is updated simply by incrementing
  120.    and then and'ing with 0x0fff (4K-1) or 0x1fff (8K-1).  Here, the 32K
  121.    buffer of inflate is used, and it works just as well to always have
  122.    a 32K circular buffer, so the index is anded with 0x7fff.  This is
  123.    done to allow the window to also be used as the output buffer. */
  124. /* This must be supplied in an external module useable like "uch slide[8192];"
  125.    or "uch *slide;", where the latter would be malloc'ed.  In unzip, slide[]
  126.    is actually a 32K area for use by inflate, which uses a 32K sliding window.
  127.  */
  128.  
  129.  
  130. /* Tables for length and distance */
  131. ush cplen2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  132.         18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  133.         35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  134.         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65};
  135. ush cplen3[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  136.         19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  137.         36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  138.         53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66};
  139. ush extra[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  140.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  141.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  142.         8};
  143. ush cpdist4[] = {1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
  144.         769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
  145.         1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
  146.         2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
  147.         2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
  148.         3649, 3713, 3777, 3841, 3905, 3969, 4033};
  149. ush cpdist8[] = {1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
  150.         1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
  151.         2817, 2945,